package com.lzy.huijinjiazu.mvp.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerMyComponent;
import com.lzy.huijinjiazu.di.module.MyModule;
import com.lzy.huijinjiazu.mvp.contract.MyContract;
import com.lzy.huijinjiazu.mvp.model.entity.Lable;
import com.lzy.huijinjiazu.mvp.presenter.MyPresenter;
import com.lzy.huijinjiazu.mvp.ui.activity.BalanceActivity;
import com.lzy.huijinjiazu.mvp.ui.activity.MyOrderActivity;
import com.lzy.huijinjiazu.mvp.ui.activity.SettingActivity;
import com.lzy.huijinjiazu.mvp.ui.activity.UserInfoActivity;
import com.lzy.huijinjiazu.mvp.ui.adapter.MyMenuAdapter;
import com.lzy.huijinjiazu.mvp.ui.listener.ScrollViewListener;
import com.lzy.huijinjiazu.mvp.ui.widget.SlideScrollView;
import com.lzy.huijinjiazu.mvp.ui.widget.decoration.SpaceDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class MyFragment extends BaseFragment<MyPresenter> implements MyContract.View, ScrollViewListener {


    @BindView(R.id.scrollView)
    SlideScrollView mScrollView;
    @BindView(R.id.fl_title)
    FrameLayout mFlTitle;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.tv_balance)
    TextView mTvBalance;
    @BindView(R.id.tv_locking_balance)
    TextView mTvLockingBalance;
    private MyMenuAdapter mAdapter;

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerMyComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .myModule(new MyModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTvBalance.setText(Html.fromHtml(ArmsUtils.getString(getContext(), R.string.symbol_moeny, "500")));
        mTvLockingBalance.setText(Html.fromHtml(ArmsUtils.getString(getContext(), R.string.symbol_moeny, "500")));
        initRecyclerView();
        initData();
        initTitle();
    }

    private void initTitle() {
        mFlTitle.setBackgroundResource(R.mipmap.bg_theme_title);
        mFlTitle.getBackground().setAlpha(0);
        mScrollView.setScrollViewListener(this);
    }

    private void initRecyclerView() {
        mAdapter = new MyMenuAdapter();
        mRvList.setHasFixedSize(true);
        mRvList.addItemDecoration(new SpaceDecoration(2, getResources().getColor(R.color.line), false, false, false));
        mRvList.setAdapter(mAdapter);
    }

    private void initData() {
        List<Lable> data = new ArrayList<>();
        data.add(new Lable(R.mipmap.my_list_1, "私行订单"));
        data.add(new Lable(R.mipmap.my_list_2, "收入明细"));
        data.add(new Lable(R.mipmap.my_list_3, "积分系统"));
        data.add(new Lable(R.mipmap.my_list_4, "我的收藏"));
        data.add(new Lable(R.mipmap.my_list_5, "地址管理"));
        data.add(new Lable(R.mipmap.my_list_6, "反馈中心"));
        mAdapter.setNewData(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBar(getActivity(), mFlTitle);
    }

    @Override
    public void onScrollChanged(SlideScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (mFlTitle == null) {
            return;
        }
        if (y > 350) {
            mFlTitle.getBackground().setAlpha(255);
            return;
        }
        if (y <= 250 && y > 0) {
            mFlTitle.getBackground().setAlpha(y);
        }
    }

    @OnClick({R.id.ll_pending_payment, R.id.ll_pending_delivery, R.id.ll_already_delivered, R.id.ll_balance, R.id.iv_setting, R.id.ll_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_pending_payment:
                ArmsUtils.startActivity(MyOrderActivity.class);
                break;
            case R.id.ll_pending_delivery:
                ArmsUtils.startActivity(MyOrderActivity.class);
                break;
            case R.id.ll_already_delivered:
                ArmsUtils.startActivity(MyOrderActivity.class);
                break;
            case R.id.ll_balance:
                ArmsUtils.startActivity(BalanceActivity.class);
                break;
            case R.id.iv_setting:
                ArmsUtils.startActivity(SettingActivity.class);
                break;
            case R.id.ll_top:
                ArmsUtils.startActivity(UserInfoActivity.class);
                break;
        }
    }

}
